package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/addressBook")
public class  AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;
    //新增地址
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        //User user = new User();
        addressBookService.save(addressBook);
//        User user = userService.getById(BaseContext.getCurrentId());
//        user.setName(addressBook.getConsignee());
//        user.setSex(addressBook.getSex());
        return R.success("新增成功");
    }

    //设置默认地址
    @PutMapping("/default")
    public R<String> defaultAddress(@RequestBody AddressBook addressBook){
        //update address_book set id_default = 0 where user_id = ?
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.set(AddressBook::getIsDefault,0);
        lqw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBookService.update(lqw);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }

    //根据id查询地址
    @GetMapping("/{id}")
    public R getAddressById(@PathVariable Long id){
//        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(AddressBook::getId,id);
//        List<AddressBook> addressBooks = addressBookService.list(lqw);
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook != null){
            return R.success(addressBook);
        }else {
            return R.error("没有找到该对象");
        }

    }

    //查询默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(lqw);
        if(addressBook != null){
            return R.success(addressBook);
        }else {
            return R.error("没有该对象信息");
        }
    }

    //查询指定用户的全部地址
    @GetMapping("/list")
    public R<List<AddressBook>> getAllAddress(){
        //select * from address_book where user_id = ?
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<AddressBook>();
        lqw.eq(BaseContext.getCurrentId() != null,AddressBook::getUserId,BaseContext.getCurrentId());
        lqw.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> addressBooks = addressBookService.list(lqw);
        return R.success(addressBooks);

    }

    //删除地址
    @DeleteMapping
    public R<String> deleteAddress(@RequestParam List<Long> ids){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getId,ids);
        addressBookService.remove(lqw);
        return R.success("删除成功");
    }

    //修改地址
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


}

